package com.wanjia.estate.service.httpservice;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.wanjia.estate.pojo.httpservice.*;
import com.wanjia.estate.properties.HttpRequestProperties;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class BdcHyZkHttpService extends AbstractHttpService {

    @Setter(onMethod_ = @Autowired)
    private HttpRequestProperties properties;

    /**
     * 获取token
     *
     * @param request 请求参数
     * @return 响应参数
     */
    public GetToken.Response token(GetToken.Request request) {
        String tokenUrl = properties.getTokenUrl();
        log.debug("request url: {}", tokenUrl);
        log.debug("request body: {}", JSONUtil.toJsonStr(request));

        RequestEntity<String> requestEntity = RequestEntity.post(tokenUrl)
                .header("Debug", "1")
                .contentType(MediaType.APPLICATION_JSON)
                .body(JSONUtil.toJsonStr(request));
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        GetToken.Response response = parseObjResponseEntity(entity, GetToken.Response.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

    /**
     * 公民基本信息比对
     *
     * @param request 请求参数
     * @return 响应参数
     */
    public Gmjbxxdb.Response gmjbxx(Gmjbxxdb.Request request) {
        String gmjbxxUrl = properties.getGmjbxxUrl();
        log.debug("request url: {}", gmjbxxUrl);
        log.debug("request body: {}", JSONUtil.toJsonStr(request));

        RequestEntity<String> requestEntity = RequestEntity.post(gmjbxxUrl)
                .header("Debug", "1")
                .header("token", token(new GetToken.Request()).getData().getToken())
                .contentType(MediaType.APPLICATION_JSON)
                .body(JSONUtil.toJsonStr(request));
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        Gmjbxxdb.Response response = parseObjResponseEntity2(entity, Gmjbxxdb.Response.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

    /**
     * 居民户信息
     *
     * @param request 请求参数
     * @return 响应参数
     */
    public Jmhxx.Response jmhxx(Jmhxx.Request request) {
        String jmhxxUrl = properties.getJmhxxUrl();
        log.debug("request url: {}", jmhxxUrl);
        log.debug("request body: {}", JSONUtil.toJsonStr(request));

        RequestEntity<String> requestEntity = RequestEntity.post(jmhxxUrl)
                .contentType(MediaType.APPLICATION_JSON)
                .header("token", token(new GetToken.Request()).getData().getToken())
                .body(JSONUtil.toJsonStr(request));
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        Jmhxx.Response response = parseObjResponseEntity(entity, Jmhxx.Response.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

    /**
     * 婚姻信息查询
     *
     * @param request 请求参数
     * @return 响应参数
     */
    public Hyxxchx.Response hyxxchx(Hyxxchx.Request request) {
        String hyxxchxUrl = properties.getHyxxchxUrl();
        log.debug("request url: {}", hyxxchxUrl);
        log.debug("request body: {}", JSONUtil.toJsonStr(request));

        RequestEntity<String> requestEntity = RequestEntity.post(hyxxchxUrl)
                .header("token", token(new GetToken.Request()).getData().getToken())
                .contentType(MediaType.APPLICATION_JSON)
                .body(JSON.toJSONString(request));
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        Hyxxchx.Response response = parseObjResponseEntity2(entity, Hyxxchx.Response.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

    /**
     * 婚姻登记信息核验（双方）
     *
     * @param request 请求参数
     * @return 响应参数
     */
    public Hydjxxhysf.Response hydjxxhysf(Hydjxxhysf.Request request) {
        String hydjxxhysfUrl = properties.getHydjxxhysfUrl();
        log.debug("request url: {}", hydjxxhysfUrl);
        log.debug("request body: {}", JSONUtil.toJsonStr(request));

        RequestEntity<String> requestEntity = RequestEntity.post(hydjxxhysfUrl)
                .header("Debug", "1")
                .header("token", token(new GetToken.Request()).getData().getToken())
                .contentType(MediaType.APPLICATION_JSON)
                .body(JSONUtil.toJsonStr(request));
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        Hydjxxhysf.Response response = parseObjResponseEntity2(entity, Hydjxxhysf.Response.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }


    /**
     * 婚姻登记信息核验（个人）
     *
     * @param request 请求参数
     * @return 响应参数
     */
    public Hydjxxhygr.Response hydjxxhygr(Hydjxxhygr.Request request) {
        String hydjxxhygrUrl = properties.getHydjxxhygrUrl();
        log.debug("request url: {}", hydjxxhygrUrl);
        log.debug("request body: {}", JSONUtil.toJsonStr(request));

        RequestEntity<String> requestEntity = RequestEntity.post(hydjxxhygrUrl)
                .header("Debug", "1")
                .header("token", token(new GetToken.Request()).getData().getToken())
                .contentType(MediaType.APPLICATION_JSON)
                .body(JSONUtil.toJsonStr(request));
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        Hydjxxhygr.Response response = parseObjResponseEntity2(entity, Hydjxxhygr.Response.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

    /**
     * 公安部-人口库基准信息查询接口
     *
     * @param request 请求参数
     * @return 响应参数
     */
    public Rkkjzxxcx.Response rkkjzxxcx(Rkkjzxxcx.Request request) {
        String rkkjzxxcxUrl = properties.getRkkjzxxcxUrl();
        log.debug("request url: {}", rkkjzxxcxUrl);
        log.debug("request body: {}", JSONUtil.toJsonStr(request));

        RequestEntity<String> requestEntity = RequestEntity.post(rkkjzxxcxUrl)
                .header("token", token(new GetToken.Request()).getData().getToken())
                .contentType(MediaType.APPLICATION_JSON)
                .body(JSONUtil.toJsonStr(request));
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        Rkkjzxxcx.Response response = parseObjResponseEntity2(entity, Rkkjzxxcx.Response.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

    /**
     * 公安部-人口库身份核查
     *
     * @param request 请求参数
     * @return 响应参数
     */
    public Rkksfhc.Response rkksfhc(Rkksfhc.Request request) {
        String rkksfhcUrl = properties.getRkksfhcUrl();
        log.debug("request url: {}", rkksfhcUrl);
        log.debug("request body: {}", JSONUtil.toJsonStr(request));

        RequestEntity<String> requestEntity = RequestEntity.post(rkksfhcUrl)
                .header("token", token(new GetToken.Request()).getData().getToken())
                .header("Debug", "1")
                .contentType(MediaType.APPLICATION_JSON)
                .body(JSONUtil.toJsonStr(request));
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        Rkksfhc.Response response = parseObjResponseEntity2(entity, Rkksfhc.Response.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }


    /**
     * 民政部-婚姻登记信息
     *
     * @param request 请求参数
     * @return 响应参数
     */
    public Mzbhydjxx.Response mzbhydjxx(Mzbhydjxx.Request request) {
        String mzbhydjxxUrl = properties.getMzbhydjxxUrl();
        log.debug("request url: {}", mzbhydjxxUrl);
        log.debug("request body: {}", JSONUtil.toJsonStr(request));

        RequestEntity<String> requestEntity = RequestEntity.post(mzbhydjxxUrl)
                .header("token", token(new GetToken.Request()).getData().getToken())
                .header("Debug", "1")
                .contentType(MediaType.APPLICATION_JSON)
                .body(JSONUtil.toJsonStr(request));
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        Mzbhydjxx.Response response = parseObjResponseEntity2(entity, Mzbhydjxx.Response.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

}
